//
//  MSMainViewController.m
//  MSJD
//
//  Created by xiangbo on 2018/9/5.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSMainViewController.h"
#import "MSNavigationController.h"
#import "MSHomeViewController.h"
#import "MSHotelViewController.h"
#import "MSCollectionViewController.h"
#import "MSUserCenterController.h"
#import "MSPhoneLoginController.h"

@interface MSMainViewController ()<UITabBarControllerDelegate>

@end

@implementation MSMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initTabbar];
}


static MSMainViewController *mainVC = nil;
+ (instancetype)sharedInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        mainVC =[[MSMainViewController alloc] init];
    });
    return mainVC;
}
- (void)initTabbar{
    NSArray *titleArray = @[@"美食",@"酒店",@"收藏",@"我"];
    NSArray *normalImageArray = @[@"tabbar_icon0",@"tabbar_icon1",@"tabbar_icon2",@"tabbar_icon3"];
    NSArray *selectImageArray = @[@"tabbar_icon0_pre",@"tabbar_icon1_pre",@"tabbar_icon2_pre",@"tabbar_icon3_pre"];
    MSHomeViewController *homeController = [[MSHomeViewController alloc] init];
    MSHotelViewController *hotel = [[MSHotelViewController alloc] init];
    MSCollectionViewController *collections = [[MSCollectionViewController alloc] init];
    MSUserCenterController *userCenter = [[MSUserCenterController alloc] init];
    
    self.viewControllers = @[[[MSNavigationController alloc] initWithRootViewController:homeController],
                             [[MSNavigationController alloc] initWithRootViewController:hotel],
                             [[MSNavigationController alloc] initWithRootViewController:collections],
                             [[MSNavigationController alloc] initWithRootViewController:userCenter]];
    for (int index = 0; index < self.viewControllers.count; index++) {
        MSNavigationController *controller = self.viewControllers[index];
        controller.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -3);
        controller.tabBarItem.title = titleArray[index];
        [controller.tabBarItem setImage:[UIImage imageNamed:normalImageArray[index]]];
        [controller.tabBarItem setSelectedImage:[[UIImage imageNamed:selectImageArray[index]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        [controller.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#999999"]} forState:UIControlStateNormal];
        [controller.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10],NSForegroundColorAttributeName:MSThemeColor} forState:UIControlStateSelected];
    }
    self.delegate = self;
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    if ([NSStringFromClass([viewController class]) isEqualToString:@"MSNavigationController"]) {
        MSNavigationController *newNav = (MSNavigationController *)viewController;
        if ([NSStringFromClass([newNav.topViewController class]) isEqualToString:@"MSUserCenterController"]) {
            if (![UserAccesser hadDefaultAccount]) {
                if (![UserAccesser hadDefaultAccount]) {
                    MSPhoneLoginController *loginVC = [[MSPhoneLoginController alloc] init];
                    MSNavigationController *nav = [[MSNavigationController alloc] initWithRootViewController:loginVC];
                    [[UIViewController currentViewController] presentViewController:nav animated:NO completion:nil];
                }
                return NO;
            }
        }
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
